package gemini.castor.ui.client.page.content.account.myorder.form;

import gemini.basic.model.Bill;
import gemini.basic.model.BillDetail;
import gemini.castor.ui.client.ServiceCatalog;
import gemini.castor.ui.client.mvp.callback.AbstractAsyncCallback;
import gemini.castor.ui.client.mvp.single.CastorWidgetDisplay;
import gemini.castor.ui.client.mvp.single.CastorWidgetPresenter;
import gemini.castor.ui.client.page.RootEvent;
import gemini.castor.ui.client.utils.GuiUtils;
import gemini.castor.ui.client.utils.PlaceConstants;

import java.util.ArrayList;
import java.util.List;

import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;

import com.google.inject.Inject;

public class MyOrderFormPresenter extends
		CastorWidgetPresenter<MyOrderFormPresenter.Display> {

	@Inject
	public MyOrderFormPresenter(Display display, EventBus globalEventBus) {
		super(display, globalEventBus);
	}

	public interface Display extends CastorWidgetDisplay {

		void setMyOrderFormModel(List<MyOrderFormModel> myOrderFormModel);
	}

	public static final Place PLACE = new Place(PlaceConstants.PLACE_MY_ORDER);
	
	private String distributorCode;

	@Override
	public void setInputObject(Object ob) {
		distributorCode = (String) ob;
	}

	@Override
	protected void onBind() {
		eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.MASK));
		ServiceCatalog.getBillService().getBillByDistributorCode(distributorCode, new AbstractAsyncCallback<List<Bill>>() {

					@Override
					public void onSuccess(List<Bill> result) {
						eventBus.fireEvent(new RootEvent(
								RootEvent.ForwardType.UN_MASK));
						if(result!=null && !result.isEmpty()){
							display.setMyOrderFormModel(createModelFromResult(result));
							display.loadData();
						}
					}
				});
	}

	protected List<MyOrderFormModel> createModelFromResult(List<Bill> bill) {
		List<MyOrderFormModel> myOrderFormModels = new ArrayList<MyOrderFormModel>();
		for (Bill eachBill : bill) {
			MyOrderFormModel myOrderFormModel = new MyOrderFormModel();
			myOrderFormModel.setCode(eachBill.getId()+"");
			myOrderFormModel.setOrderDate(GuiUtils.format(eachBill.getProcessingDate()));
			myOrderFormModel.setDiscountScale(eachBill.getDiscountScaleValue()+"");
			myOrderFormModel.setBillingAdd(GuiUtils.addressToString(eachBill.getAddressByBillingAddressId()));
			myOrderFormModel.setShippingAdd(GuiUtils.addressToString(eachBill.getAddressByShippingAddressId()));
			myOrderFormModel.setOrderMonth(GuiUtils.format(eachBill.getProcessingDate()));
			myOrderFormModel.setPaymentBy(eachBill.getPayment().getPaymentType());
			myOrderFormModel.setPurchaseName(eachBill.getPurchasedBy());
			myOrderFormModel.setStatus("Complete");
			myOrderFormModel.setDistributorCode(distributorCode);
			List<MyOrderProductDetail> productList = new ArrayList<MyOrderProductDetail>();
			short point = 0;
			for (BillDetail billDetail : eachBill.getBillDetails()) {
				MyOrderProductDetail myProductDetail = new MyOrderProductDetail();
				myProductDetail.setProduct(billDetail.getProduct());
				myProductDetail.setQuantity(billDetail.getQuantity());
				point += (short) (billDetail.getProduct().getVolumePoint()*billDetail.getQuantity());
				productList.add(myProductDetail);
			}
			myOrderFormModel.setPoint(point+"");
			myOrderFormModel.setProductList(productList);
			
			myOrderFormModels.add(myOrderFormModel);
		}
		return myOrderFormModels;
	}
	

	@Override
	public Place getPlace() {
		return PLACE;
	}
}
